<template>
  <div class="w-1200px mx-auto">
    <div class="flex justify-between">
      <div class="w-300px h-750px bg-white rounded-8px relative">
        <div class="mt-24px mx-24px">
          <div class="flex border-1px border-solid border-top-0 border-left-0 border-right-0 pb-15px">
            <div class="w-4px h-22px bg-red-500 mr-15px"></div>
            <div class="text-20px font-weight-600">我的复习计划</div>
          </div>
          <div class="mt-15px px-15px">
            <div class="text-16px font-weight-600">{{ distanceText }}</div>
            <div class="flex justify-center my-25px">
              <div class="dayBg w-34px h-47px text-center leading-47px self-end mx-2px font-weight-600 text-42px text-orange-400" v-for="item in distance">{{ item }}</div>
              <div class="self-end ml-5px">天</div>
            </div>
          </div>
          <div class="mt-15px px-15px">
            <div class="flex justify-between text-14px font-weight-500">
              <div>知识点：{{ pointData.point_count }} 个</div>
              <div class="text-red-500">已复习：{{ pointData.learn_count }} 个</div>
            </div>
            <div class="mt-15px">
              <el-progress :stroke-width="12" striped :duration="duration" striped-flow :percentage="pointData.percent" />
            </div>
          </div>
          <div class="mt-30px">
            <div>
              <div class="leading-25px mb-30px text-14px px-15px inline-block rounded-5px bg-orange-500 text-white">今日任务</div>
            </div>
            <div><!--教材-->
              <div class="flex">
                <div class="px-15px text-14px leading-25px bg-red-300 text-white rounded-5px mr-10px self-center">教材</div>
                <div class="text-14px text-dark-500 self-center">{{ lessonData.name }}</div>
              </div>
              <div class="relative">
                <div class="text-red-100 leading-0 absolute w-22px h-22px left-15px -top-15px">
                  <el-icon :size="22"><CaretTop /></el-icon>
                </div>
                <div class="bg-red-100 h-110px p-15px pt-12px mt-20px rounded-5px">
                  <div class="" v-for="(item,index) in todayTask.needChapter">
                    <div  v-if="index < 2">
                      <div class="text-12px mb-3px text-dark-500">{{ item.teaching_name }}</div>
                      <div class="text-12px whitespace-nowrap mb-3px text-gray-500">{{ item.chapter_name }}</div>
                      <div class="text-12px whitespace-nowrap mb-5px text-gray-500">{{ item.section_name }}</div>
                    </div>
                    
                  </div>
                </div>
              </div>
            </div>
            <div class="mt-50px"><!--习题-->
              <div class="flex">
                <div class="px-15px text-14px leading-25px bg-red-300 text-white rounded-5px mr-10px self-center">习题</div>
                <div class="text-14px text-dark-500 self-center">{{ lessonData.name }}</div>
              </div>
              <div class="relative">
                <div class="text-red-100 leading-0 absolute w-22px h-22px left-15px -top-15px">
                  <el-icon :size="22"><CaretTop /></el-icon>
                </div>
                <div class="bg-red-100 h-110px p-15px pt-12px mt-20px rounded-5px">
                  <div class="" v-for="(item,index) in todayTask.needTopics">
                    <div  v-if="index < 5">
                      <div class="text-12px h-18px w-220px leading-18px text-dark-500 truncate">{{ item.topic_title }}</div>
                    </div>
                    
                  </div>
                </div>
              </div>
            </div>

            <div class="text-right text-14px text-dark-500 absolute right-15px bottom-20px">
              <router-link :to="{ path: '/plan'}">查看更多》</router-link>
            </div>
          </div>
        </div>
      </div>
      <div class="w-570px h-750px">
        <div class="h-350px bg-white rounded-8px relative">
          <div class="pt-24px px-24px">
            <div class="flex border-1px border-solid border-top-0 border-left-0 border-right-0 pb-15px">
              <div class="w-4px h-22px bg-red-500 mr-15px"></div>
              <div class="text-20px font-weight-600">学习进度</div>
            </div>

            <div class="flex mt-10px justify-between flex-wrap text-right">
              <div class="border-solid border-1 w-170px mt-20px rounded-5px">
                <div class="m-15px flex justify-between">
                  <div class="self-center">
                    <el-progress type="circle" :width="45" :percentage="percentData.topic_percent" color="#f56c6c" />
                  </div>
                  <div class="self-center">
                    <div class="text-14px text-dark-500 mb-5px">{{ percentData.do_topic_count }}/{{ percentData.topic_count }}</div>
                    <div class="text-12px text-gray-500">答题进度</div>
                  </div>
                </div>
              </div>

              <div class="border-solid border-1 w-170px mt-20px rounded-5px">
                <div class="m-15px flex justify-between">
                  <div class="self-center">
                    <el-progress type="circle" :width="45" :percentage="percentData.have_learn_chapter_topic_percent"  color="#e6a23c" />
                  </div>
                  <div class="self-center">
                    <div class="text-14px text-dark-500 mb-5px">{{ percentData.have_learn_chapter_topic_count }}/{{ percentData.topic_count }}</div>
                    <div class="text-12px text-gray-500">章节练习进度</div>
                  </div>
                </div>
              </div>

              <div class="border-solid border-1 w-170px mt-20px rounded-5px">
                <div class="m-15px flex justify-between">
                  <div class="self-center">
                    <el-progress type="circle" :width="45" :percentage="percentData.right_topic_percent"  color="#5cb87a" />
                  </div>
                  <div class="self-center">
                    <div class="text-14px text-dark-500 mb-5px">{{ percentData.right_topic_count }}/{{ percentData.right_and_wrong }}</div>
                    <div class="text-12px text-gray-500">正确率</div>
                  </div>
                </div>
              </div>

              <div class="border-solid border-1 w-170px mt-20px rounded-5px">
                <div class="m-15px flex justify-between">
                  <div class="self-center">
                    <el-progress type="circle" :width="45" :percentage="pointData.point_learn_percent"  color="#1989fa" />
                  </div>
                  <div class="self-center">
                    <div class="text-14px text-dark-500 mb-5px">{{ percentData.point_learn_count }}/{{ percentData.point_count }}</div>
                    <div class="text-12px text-gray-500">知识点进度</div>
                  </div>
                </div>
              </div>

              <div class="border-solid border-1 w-170px mt-20px rounded-5px">
                <div class="m-15px flex justify-between">
                  <div class="self-center">
                    <el-progress type="circle" :width="45" :percentage="percentData.video_learn_percent"  color="#6f7ad3" />
                  </div>
                  <div class="self-center">
                    <div class="text-14px text-dark-500 mb-5px">{{ percentData.video_have_learn_count }}/{{ percentData.video_count }}</div>
                    <div class="text-12px text-gray-500">视频观看进度</div>
                  </div>
                </div>
              </div>

              <div class="border-solid border-1 w-170px mt-20px rounded-5px">
                <div class="m-15px flex justify-between">
                  <div class="self-center">
                    <el-progress type="circle" :width="45" :percentage="percentData.percent" />
                  </div>
                  <div class="self-center">
                    <div class="text-14px text-dark-500 mb-5px">{{ percentData.learn_count }}/{{ percentData.chapter_count }}</div>
                    <div class="text-12px text-gray-500">教材学习进度</div>
                  </div>
                </div>
              </div>
              


            </div>
          </div>

          <div class="text-right text-14px text-dark-500 absolute right-15px bottom-20px">
            查看更多》
          </div>
        </div>
        <div class="h-385px mt-15px flex justify-between">
          <div class="w-277px h-385px bg-white rounded-8px relative">
            <div class="pt-24px px-24px">
              <div class="flex border-1px border-solid border-top-0 border-left-0 border-right-0 pb-15px">
                <div class="w-4px h-22px bg-red-500 mr-15px"></div>
                <div class="text-20px font-weight-600">学习情况</div>
              </div>

              <div><!--已掌握-->
                <div class="flex mt-10px">
                  <div class="px-15px text-14px leading-25px bg-red-300 text-white rounded-5px mr-10px self-center">我已会</div>
                </div>
                <div class="relative">
                  <div class="text-red-100 leading-0 absolute w-22px h-22px left-15px -top-15px">
                    <el-icon :size="22"><CaretTop /></el-icon>
                  </div>
                  <div class="bg-red-100 h-70px p-15px mt-20px rounded-5px">
                    
                        <div class="text-12px mb-3px text-dark-500 truncate">{{ myLevelData.iCanEnd.teaching_name }}{{ myLevelData.iCanEnd.teaching_name2 }}{{ myLevelData.iCanEnd.teaching_name3 }}</div>
                        <div class="text-12px whitespace-nowrap mb-3px text-gray-500 truncate">{{ myLevelData.iCanEnd.chapter_name }}</div>
                        <div class="text-12px whitespace-nowrap mb-5px text-gray-500 truncate">{{ myLevelData.iCanEnd.section_name }}</div>
                      
                  </div>
                </div>
              </div>

              <div><!--未掌握-->
                <div class="flex mt-20px">
                  <div class="px-15px text-14px leading-25px bg-gray-300 text-white rounded-5px mr-10px self-center">我不会</div>
                </div>
                <div class="relative">
                  <div class="text-gray-100 leading-0 absolute w-22px h-22px left-15px -top-15px">
                    <el-icon :size="22"><CaretTop /></el-icon>
                  </div>
                  <div class="bg-gray-100 h-70px p-15px mt-20px rounded-5px">
                    <div class="text-12px mb-3px text-dark-500 truncate">{{ myLevelData.iCanntEnd.teaching_name }}{{ myLevelData.iCanntEnd.teaching_name2 }}{{ myLevelData.iCanntEnd.teaching_name3 }}</div>
                    <div class="text-12px whitespace-nowrap mb-3px text-gray-500 truncate">{{ myLevelData.iCanntEnd.chapter_name }}</div>
                    <div class="text-12px whitespace-nowrap mb-5px text-gray-500 truncate">{{ myLevelData.iCanntEnd.section_name }}</div>
                  </div>
                </div>
              </div>

            </div>
            <div class="text-right text-14px text-dark-500 absolute right-15px bottom-20px">
              查看更多》
            </div>
          </div>
          <div class="w-277px h-385px bg-white rounded-8px relative">
            <div class="pt-24px px-24px">
              <div class="flex border-1px border-solid border-top-0 border-left-0 border-right-0 pb-15px">
                <div class="w-4px h-22px bg-red-500 mr-15px"></div>
                <div class="text-20px font-weight-600">每周测试</div>
              </div>

              <div class="my-10px">
                <block v-for="(item, index) in weekTopicData" :key="index">
                  <div class="border border-top-0 border-left-0 border-right-0 mb-10px" v-if="index < 5">
                    <div class="text-14px leading-20px h-20px w-220px truncate">{{ item.topic_title }}</div>
                    <div class="text-12px text-gray-300 leading-20px" v-if="item.topic_type == 1">单选题</div>
                    <div class="text-12px text-gray-300 leading-20px" v-if="item.topic_type == 2">多选题</div>
                    <div class="text-12px text-gray-300 leading-20px" v-if="item.topic_type == 3">问答题</div>
                  </div>
                </block>
                <div class="text-center text-14px text-dark-500" v-if="!weekTopicData.length">您本周还未答题，暂无数据</div>
                
              </div>

            </div>
            <div class="text-right text-14px text-dark-500 absolute right-15px bottom-20px">
              <router-link :to="{ path: '/exam'}">查看更多》</router-link>
            </div>
          </div>
        </div>
      </div>
      <div class="w-300px h-750px">
        <div class="h-350px bg-white rounded-8px relative">

          <div class="pt-24px px-24px">
            <div class="flex border-1px border-solid border-top-0 border-left-0 border-right-0 pb-15px">
              <div class="w-4px h-22px bg-red-500 mr-15px"></div>
              <div class="text-20px font-weight-600">最新公告</div>
            </div>

            <div class="my-10px">
                <div class="border border-top-0 border-left-0 border-right-0 mb-10px" v-for="(item,index) in noticeData" :key="index">
                  <div class="text-14px leading-24px">{{ item.post_title }}</div>
                  <div class="text-12px text-gray-300 leading-24px">{{ item.published_time }}</div>
                </div>
                
              </div>

          </div>
          <div class="text-right text-14px text-dark-500 absolute right-15px bottom-20px">
            查看更多》
          </div>
        </div>
        <div class="h-385px mt-15px bg-white rounded-8px relative">
          <div class="pt-24px px-24px">
            <div class="flex border-1px border-solid border-top-0 border-left-0 border-right-0 pb-15px">
              <div class="w-4px h-22px bg-red-500 mr-15px"></div>
              <div class="text-20px font-weight-600">同学社区</div>
            </div>

            <div class="my-20px">
              <block  v-for="(item,index) in communityData.list" :key="index">
                <div class="border border-top-0 border-left-0 border-right-0 mb-10px linkText" v-if="index < 3" @click="toCommunityDetail(item)">
                  <div class="text-14px leading-24px">{{ item.com_title }}</div>
                  <div class="text-14px text-gray-500 leading-24px">{{ item.nickname }}</div>
                  <div class="text-12px text-gray-300 leading-24px">{{ item.com_addtime }}</div>
                </div>
              </block>
                
                
              </div>

          </div>
          <div class="text-right text-14px text-dark-500 absolute right-15px bottom-20px">
            <router-link :to="{ path: '/interaction'}">查看更多》</router-link>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
  import { useUserStore } from '../../stores/user'
  import { configStore } from '../../stores/config'
  import { systemApi,userApi,httpApi } from '../../api/api'
  import { ElMessage } from 'element-plus'
  import { ref,onMounted,watch } from "vue"
  import { diffTwoDates,getToday } from '../../utils/tool'
  import { useRouter } from 'vue-router'

  const router = useRouter()
  const config = configStore()
  const lessonData = ref({})
  let distanceText = ref("")
  let distance = ref([])
  let pointData = ref([])
  let duration = ref(0)
  const todayTask = ref({})
  const percentData = ref({})
  const noticeData = ref([])
  const communityData = ref([])
  const weekTopicData = ref([])
  const myLevelData = ref({
    iCanEnd:{
      chapter_name:"",
      section_name:"",
      teaching_name:"",
      teaching_name2:"",
      teaching_name3:""
    },
    iCanntEnd:{
      chapter_name:"",
      section_name:"",
      teaching_name:"",
      teaching_name2:"",
      teaching_name3:""
    }
  })

  const init = async ()=>{
    lessonData.value = config.getBook()
    let today = getToday()
    let resLessonExamTime = await httpApi.getLessonExamtime()
    const configDate = config.getConfig()
    configDate.examTime = resLessonExamTime.data.exam_time

    let examDate = configDate.examTime
      //config.setConfig(configDate) ///设置缓存的考试时间
    let diffDays = diffTwoDates(today,examDate)
      //console.log("相差几天",diffDays)
    if(diffDays >= 0){
      distance.value = diffDays.toString().split("")
      distanceText.value = "距离考试还有："
    }else{
      distanceText.value = "考试已超期："
      distance.value = Math.abs(diffDays).toString().split("")
    }
    
    getPointData()
    getDayTask()
    getPercentData()
    getLastNotice()
    getCommunityList()
    getWeekTopic()
    getMyLevel()

  }

  const getPointData = async ()=>{ //获取知识点
    
    let res = await httpApi.getPoint(lessonData.value)
    if(res.code){
      pointData.value = res.data
      
      duration.value = res.data.percent / 25
    }
  }

  const getDayTask = async()=>{
    let res = await httpApi.getDayTask(lessonData.value)
    if(res.code){
      todayTask.value = res.data
    }
    //console.log("今日任务",todayTask.value)
  }


  //获取学习进度
  const getPercentData = async ()=>{
    let res = await httpApi.getPercentData(lessonData.value)
    if(res.code){
      percentData.value = res.data
      //console.log("学习进度",percentData.value)
    }
  
  }
  //获取最新公告
  const getLastNotice = async ()=>{
    let res = await httpApi.getLastNotice()
    if(res.code){
      noticeData.value = res.data
      //console.log("最新公告",noticeData.value)
    }
  }
  //同学社区列表
  const getCommunityList = async ()=>{
    let params = {
      page:1
    }
    let res = await httpApi.getCommunityList(params)
    if(res.code){
      communityData.value = res.data
      //console.log("同学社区",communityData.value)
    }
  }
  //获取本周题目列表
  const getWeekTopic = async ()=>{
    let res = await httpApi.getWeekTopic(lessonData.value)
    if(res.code){
      weekTopicData.value = res.data
      //console.log("本周题目",weekTopicData.value)
    }
  
  }
  
  //获取我的水平数据
  const getMyLevel = async ()=>{
    let res = await httpApi.myLevel(lessonData.value)
    if(res.code){
      let levelData = res.data
      if(levelData.iCan.length > 0){
        myLevelData.value.iCanEnd = myLevelData.value.iCan[myLevelData.value.iCan.length-1]
      }
      //myLevelData.value = res.data
      
      if(levelData.iCannt.length > 0){
        myLevelData.value.iCanntEnd = myLevelData.value.iCannt[myLevelData.value.iCannt.length-1]
      }
      

      console.log("我的水平",res.data)
    }
  
  }

  const toCommunityDetail = (item)=>{
    router.push({name:"ClassmateDetail",query:{com_id:item.com_id}})
  }
  

  // 科目变化时加载
  watch(() => config.$state.currentBook.book_id, () => {
    init()
  })

  onMounted(()=>{
    init();
  })
</script>
<style scoped>
  .dayBg{
    background: url("../../../public/images/day-bg.png") 0 0 no-repeat;
  }
  .linkText{
    cursor: pointer;
  }
</style>